﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DTTB.Common;
using DTTB.Domain;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.BussinessDelegate.Base;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Models.FormBean.PhanQuyen;
using DTTB.DTO;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Models.LinQ.PhanQuyen;
using DTTB.SERVICE;

namespace DTTB.MV2.QUANLYNHANSUTIENLUONG.BussinessDelegate.PhanQuyen
{
    public class JobTitlesServiceDelegate : BaseServiceDelegate<JobTitlesDto, JobTitles>
    {
        PhanQuyenDataContext PhanQuyenDB = new PhanQuyenDataContext();
        JobTitlesService JobTitlesService = new JobTitlesService();


        public JobTitlesDto GetJobTitlesById(string id)
        {
            return JobTitlesService.GetJobTitlesById(id);
        }

        public JobTitlesDto BuildNewJobTitles(JobTitlesForm form)
        {
            JobTitlesDto newJobTitle = new JobTitlesDto();
            newJobTitle.JobTitle = form.JobTitle;
            newJobTitle.Description = form.Description;
            newJobTitle.CreateDate = DateTime.Now;
            newJobTitle.UpdateDate = DateTime.Now;
            newJobTitle.CreateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
            newJobTitle.UpdateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
            newJobTitle.IsDelete = false;

            return newJobTitle;
        }

        public JobTitlesDto BuildEditJobTitles(JobTitlesForm form)
        {
            JobTitlesDto newJobTitle = JobTitlesService.GetJobTitlesById(form.JobTitleID);
            newJobTitle.JobTitle = form.JobTitle;
            newJobTitle.Description = form.Description;
            //newJobTitle.CreateDate = DateTime.Now;
            newJobTitle.UpdateDate = DateTime.Now;
            //newJobTitle.CreateBy = SessionDetail.USERID;
            newJobTitle.UpdateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
            //newJobTitle.IsDelete = false;

            return newJobTitle;
        }

        public void BuildSelectListForJobTitlesForm(JobTitlesForm form)
        {
            form.TieuChiSearchDropDownList = BuildSelectListSearchJobTitles();
        }

        public bool AddNewJobTitles(JobTitlesDto jobTitlesDto)
        {
            return JobTitlesService.AddNewJobTitles(jobTitlesDto);
        }

        public bool AddNewJobTitles(JobTitlesDto jobTitlesDto, ref int idReturn)
        {
            return JobTitlesService.AddNewJobTitles(jobTitlesDto, ref idReturn);
        }

        public bool Update(JobTitlesDto jobTitlesDto)
        {
            return JobTitlesService.Update(jobTitlesDto);
        }

        public bool ShiftDelete(string jobTitlesId)
        {
            return JobTitlesService.ShiftDelete(jobTitlesId);
        }

        public bool Delete(string jobTitlesId)
        {
            return JobTitlesService.Delete(jobTitlesId);
        }

        public JobTitlesDto GetJobTitleByName(string jobTitleName)
        {
            return JobTitlesService.GetJobTitleByName(jobTitleName);
        }

        public SelectList BuildJobTitleSelectList()
        {
            IList<JobTitlesDto> list;

            list = JobTitlesService.GetAllEntities(); //test

            IList<object> selectList = new List<object> { new { value = 0, Text = "[-- Chọn chức vụ --]" } };

            foreach (var dto in list)
            {
                selectList.Add(new { value = dto.ID, Text = dto.JobTitle });
            }

            return new SelectList(selectList, "Value", "Text");
        }
       


        #region Phần này chứa những hàm chuyển sang dùng LinQ

        /// <summary>
        /// Lấy tất cả chức vụ chưa đánh dấu xóa trong hệ thống
        /// </summary>
        /// <returns>Danh sách chứ vụ</returns>
        public IList<TB_JobTitle> GetAllJobTitles()
        {
            return PhanQuyenDB.TB_JobTitles.Where(p => p.IsDelete == false).OrderBy(p => p.JobTitle).ToList();
        }


        //search

        /// <summary>
        /// Build selectList - Tiêu chí tìm kiếm Chức vụ
        /// </summary>
        /// <returns>SelectList</returns>
        public SelectList BuildSelectListSearchJobTitles()
        {
            IList<object> tieuChiSearchList = new List<object>();
            tieuChiSearchList.Add(new { value = 1, Text = "Chức vụ" });
            //tieuChiSearchList.Add(new{value = 1, Text = "..."});

            return new SelectList(tieuChiSearchList, "Value", "Text");
        }
        /// <summary>
        /// Tìm kiếm chức vụ theo một tiêu chí
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns>Danh sách các chức vụ</returns>
        public IList<TB_JobTitle> SearchJobTitles(int key, string value)
        {
            IList<TB_JobTitle> jobTitleList = new List<TB_JobTitle>();
            var jobtitles = from jobtitle in PhanQuyenDB.TB_JobTitles
                        where jobtitle.IsDelete == false
                        select jobtitle;
            if (key == 1) // Tìm Chức vụ theo Tên
                jobTitleList = jobtitles.Where(p => p.JobTitle.Contains(value)).OrderBy(p => p.JobTitle).ToList();

            return jobTitleList;
        }

        #endregion
    }
}
